package com.kongwg.java;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author kongwg
 * @create 2020-12-18 15:35
 */

/**
 * 对图片的复制
 */
public class FileInputOutputTest {
    @Test
    public void ImagesReverseTest() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //实例化file
            File srcFile = new File("爱情与友情.jpg");
            File descFile = new File("爱情与友情2.jpg");
            //实例化输入输出流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(descFile);

            //操作数据
            byte[] bbuf = new byte[10];
            int len;//记录每次读取的字节数
            while ((len = fis.read(bbuf)) != -1) {
                fos.write(bbuf, 0, len);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭数据流
            try {
                if (fis != null) {
                    fis.close();

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) {

                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }

    public void copyFileStream(String srcPath,String descPath){


        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //实例化file
            File srcFile = new File(srcPath);
            File descFile = new File(descPath);
            //实例化输入输出流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(descFile);

            //操作数据
            byte[] bbuf = new byte[1024];
            int len;//记录每次读取的字节数
            while ((len = fis.read(bbuf)) != -1) {
                fos.write(bbuf, 0, len);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭数据流
            try {
                if (fis != null) {
                    fis.close();

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) {

                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }



    }
    @Test
    public void CopyFileTest(){
        long start = System.currentTimeMillis();
        String srcPath = "C:\\Users\\Administrator\\Desktop/千与千寻纯音乐.mp3";
        String descPath = "C:\\Users\\Administrator\\Desktop/千与千寻纯音乐test.mp3";
        copyFileStream(srcPath,descPath);
        long end = System.currentTimeMillis();

        System.out.println("copy time is " + (end - start));
    }
}
